package com.yanqu.road.dao.impl.servercenter;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.servercenter.MallRebate;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

public class MallRebateDaoImpl extends TempDao {
    public Map<Integer, MallRebate> getMallRebateMap(int channelId) {
        Map<Integer, MallRebate> dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_mall_rebate where channelId=? or channelId=0";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());

            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, channelId);
            rs = call.executeQuery();
            while (rs.next()) {
                MallRebate data = new MallRebate();
                data.setMallId(rs.getInt("mallId"));
                data.setRebateRatio(rs.getInt("rebateRatio"));
                dataMap.put(data.getMallId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return dataMap;
    }
}
